﻿namespace Sai.Core.Introspection
{
	using System;
	using System.Text;

	public class FieldDeclaration : DataMember, IExactlyMatch<FieldDeclaration>, ILooselyMatch<FieldDeclaration>, ISourceCodeProvider 
	{
		public FieldDeclaration(TypeName typeName)
			: base(typeName)
		{
		}

		public FieldDeclaration(string name, TypeName typeName, ObsoleteMarker obsolete, TypeName declaringType)
			: base(typeName, obsolete, declaringType)
		{
			this.Name = name;
		}

		public string Name { get; set; }

        public bool IsProtected { get; set; }

        public bool IsConstant { get; set; }

		public override string ToString()
		{
			var builder = new StringBuilder();

			builder.AppendFormat(
				"{0} {1}",
				this.TypeName,
				this.Name);

			return builder.ToString();
		}

		public bool ExactMatchFor(FieldDeclaration other)
		{
			bool exact = string.Compare(
				this.ToString(),
				other.ToString(),
				StringComparison.Ordinal) == 0;

			return exact;
		}

		public SourceCode ToCode()
		{
            SourceCode code = new SourceCode();

            code.Add(this.TypeName.ToCode());
            code.Add(new IdentifierTag(this.Name));

            return code;
		}

		public bool LooseMatchFor(FieldDeclaration other)
		{
            // matches if the names are the same
			return string.Compare(
                this.Name,
                other.Name,
				StringComparison.Ordinal) == 0;
		}
	}
}
